package service;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.springframework.web.multipart.MultipartFile;

import util.Utilities;
import dao.BoardDAO;
import dto.BoardDTO;
import dto.ListBoardDTO;
import dto.ListSalePageDTO;
import dto.PagingDTO;
import dto.SaleListDTO;

public class BoardService {
	private BoardDAO dao;
	private String fileDirectory;

	public BoardService(BoardDAO dao, String fileDirectory) {
		this.dao = dao;
		this.fileDirectory = fileDirectory;
	}

	public void insertBoard(BoardDTO bdto) throws Exception{
		int no = dao.selectBoardNo();
		MultipartFile upfile = bdto.getUpfile();
		String pictureFilename = null;
		
		if (upfile!=null&&!upfile.isEmpty()) {
			String fileName = upfile.getOriginalFilename();
			String ext = fileName.substring(fileName.lastIndexOf("."));
			long time = System.currentTimeMillis();
			pictureFilename = time + ext;
			bdto.setPictureName(pictureFilename);
			upfile.transferTo(new File(fileDirectory, pictureFilename));
		}
		
		bdto.setNo(no);
		bdto.setWritedate(Utilities.getNow());
		bdto.setContent(Utilities.changeContentForDB(bdto.getContent()));
		bdto.setRefamily(no);
		
		dao.insetBoard(bdto);
	}

	public List selectAllBoardNoti() throws Exception {
		ArrayList<BoardDTO> list = (ArrayList<BoardDTO>) dao.selectAllBoardNoti();
		ArrayList<BoardDTO> list2 = new ArrayList<BoardDTO>();
		
		for (BoardDTO bdto : list) {
			bdto.setWritedate(Utilities.changeDayFormat(bdto.getWritedate()));
			list2.add(bdto);
		}
		
		return list2;
	}

	public BoardDTO boardContent(int no) {
		dao.plusViewCount(no);
		
		BoardDTO bdto = dao.boardContent(no);
		
		return bdto;
	}
	
	public void deleteBoard(int no){
		dao.deleteBoard(no);
	}
	
	public void updateBoard(BoardDTO bdto) throws Exception{
		
		MultipartFile upfile = bdto.getUpfile();
		String pictureFilename = null;
		if (upfile!=null&&!upfile.isEmpty()) {
			String fileName = upfile.getOriginalFilename();
			String ext = fileName.substring(fileName.lastIndexOf("."));
			long time = System.currentTimeMillis();
			pictureFilename = time + ext;
			bdto.setPictureName(pictureFilename);
			upfile.transferTo(new File(fileDirectory, pictureFilename));
		}
		
		bdto.setWritedate(Utilities.getNow());
		bdto.setContent(Utilities.changeContentForDB(bdto.getContent()));
		dao.updateBoard(bdto);
	}
	
	public void replyBoard(BoardDTO bdto){

		BoardDTO bdto2 = dao.boardContent(bdto.getNo());
		bdto.setRefamily(bdto2.getRefamily());
		bdto.setRestep(bdto2.getRestep());
		bdto.setRelevel(bdto2.getRelevel());
		
		dao.replyBoard(bdto);

		bdto.setRefamily(bdto.getRefamily());
		bdto.setRestep(bdto.getRestep()+1);
		bdto.setRelevel(bdto.getRelevel()+1);
		bdto.setNo(dao.selectBoardNo());
		bdto.setWritedate(Utilities.getNow());
		bdto.setContent(Utilities.changeContentForDB(bdto.getContent()));
		
		dao.insetBoard(bdto);
		
	}

	public void deleteFile(BoardDTO bdto) {
		File file = new File(fileDirectory, dao.boardContent(bdto.getNo()).getPictureName());
		file.delete();
		bdto.setPictureName(null);
		dao.updateBoard(bdto);
	}
	
	public List<BoardDTO> selectBoardByPaging(int page, String contentGroup){
		return dao.paging(page, contentGroup);
	}
	
	public ListBoardDTO paging(int page, String contentGroup){
		PagingDTO pdto = new PagingDTO(dao.pageNum(contentGroup), page);
		ArrayList<BoardDTO> list = (ArrayList<BoardDTO>) dao.paging(page, contentGroup);
		for(BoardDTO bdto : list){
			bdto.setWritedate(Utilities.changeDayFormat(bdto.getWritedate()));
		}
		
		return new ListBoardDTO(list, pdto);
	}
	
	public ListBoardDTO pagingSearch(int page, BoardDTO bdto){
		PagingDTO pdto = new PagingDTO(dao.pageNum(bdto.getContentGroup()), page);
		ArrayList<BoardDTO> list = (ArrayList<BoardDTO>) dao.pagingSearch(page, bdto);
		for(BoardDTO b : list){
			b.setWritedate(Utilities.changeDayFormat(b.getWritedate()));
		}
		
		return new ListBoardDTO(list, pdto);
	}
}